package gconfig

import (
	"log"
	"os"
	"strings"

	"github.com/joho/godotenv"
)

//Cnf 环境变量map
var Cnf map[string]string

func readEnvConfig() (string, []string) {
	err := godotenv.Load("config.env")
	//如果读取不到
	if err != nil {
		return "development", nil
	}
	env := os.Getenv("GODAILYLIB_ENV")
	configKeyMap := strings.Split(os.Getenv("CONFIGKEY_MAP"), ",")
	return env, configKeyMap
}

//ReadConfig 读取配置文件配置
func ReadConfig() map[string]string {
	env, configKeyMap := readEnvConfig()

	err := godotenv.Load(env + ".env")
	if err != nil {
		log.Fatal(err)
	}
	configMap := make(map[string]string)
	//读取配置
	for _, configKey := range configKeyMap {
		configMap[configKey] = os.Getenv(configKey)
	}
	log.Println("config init,config map is : ", configMap)
	Cnf = configMap
	return configMap
}
